<?php 
namespace XFCore\traits\user;
trait MainTrait{
    public function consumeall(){
        if($this->islogin_user !== 1)exitjson(-1,'请先登录');
        $sql = "SELECT 
            COUNT(*) as total_orders,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as succ_orders,
            SUM(CASE WHEN status = 1 THEN money ELSE 0 END) as total_money
            FROM pre_order 
            WHERE pid = :pid";
       
        $result = safeSqlQuery($sql, ['pid' => $this->uid] , 'select' ,['single_row'=>true])['result'];
        $data = [
            'money' => $this->userrow['money'] ?:0,
            'succorder' => (int)$result['succ_orders']?:0,
            'order' => (int)$result['total_orders']?:0,
            'succmoney' => (float)$result['total_money']?:0
        ];
        exitjson(200,'success',['data'=>$data]);
    }
    public function maintypegetDataList($get) {
        if($this->islogin_user !== 1) exitjson(-1,'请先登录');
        
        // 使用子查询获取订单统计
        $result = $this->db->complexSelect(
            "qrlist q",
            "q.*, 
             IFNULL(o1.total_money, 0) as money,
             IFNULL(o2.succ_money, 0) as succmoney,
             IFNULL(o1.order_count, 0) as `order`,
             IFNULL(o2.succ_count, 0) as succorder",
            ['q.pid' => $this->uid],
            [
                'join' => [
                    [
                        'type' => 'LEFT',
                        'table' => '(SELECT qr_id, COUNT(*) as order_count, SUM(money) as total_money 
                                   FROM pre_order 
                                   GROUP BY qr_id) o1',
                        'on' => 'q.id = o1.qr_id'
                    ],
                    [
                        'type' => 'LEFT',
                        'table' => '(SELECT qr_id, COUNT(*) as succ_count, SUM(money) as succ_money 
                                   FROM pre_order 
                                   WHERE status = 1 
                                   GROUP BY qr_id) o2',
                        'on' => 'q.id = o2.qr_id'
                    ]
                ]
            ]
        );
        
        if(empty($result['count'])) {
            exitjson(0, 'ok', ['count' => 0, 'data' => []]);
        }
        
        $data = [];
        foreach($result['result'] as $row) {
            $data[] = [
                'id' => $row['id'],
                'typename' => $row['type'] ?? '未知',
                'beizhu' => $row['name'] ?? '无',
                'status' => $row['status'],
                'money' => number_format($row['money'], 2),
                'succmoney' => number_format($row['succmoney'], 2),
                'order' => intval($row['order']),
                'succorder' => intval($row['succorder'])
            ];
        }
        
        exitjson(0, 'ok', [
            'count' => count($data),
            'data' => $data
        ]);
    }
    
    public function mainpaygetDataList($get) {
       if($this->islogin_user !== 1) exitjson(-1,'请先登录');
       
       // 使用子查询获取各时间段的订单统计
       $result = $this->db->complexSelect(
           "qrlist q",
           "q.id,
            IFNULL(today.count, 0) as today_count,
            IFNULL(today.money, 0) as today_money,
            IFNULL(today_succ.money, 0) as today_succmoney,
            IFNULL(week.count, 0) as week_count,
            IFNULL(week.money, 0) as week_money, 
            IFNULL(week_succ.money, 0) as week_succmoney,
            IFNULL(month.count, 0) as month_count,
            IFNULL(month.money, 0) as month_money,
            IFNULL(month_succ.money, 0) as month_succmoney",
           ['q.pid' => $this->uid],
           [
               'join' => [
                   // 今日订单统计
                   [
                       'type' => 'LEFT',
                       'table' => "(SELECT qr_id, COUNT(*) as count, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE TO_DAYS(addtime) = TO_DAYS(NOW())
                                  GROUP BY qr_id) today",
                       'on' => 'q.id = today.qr_id'
                   ],
                   // 今日成功订单
                   [
                       'type' => 'LEFT',
                       'table' => "(SELECT qr_id, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE TO_DAYS(addtime) = TO_DAYS(NOW()) AND status = 1
                                  GROUP BY qr_id) today_succ",
                       'on' => 'q.id = today_succ.qr_id'
                   ],
                   // 本周订单统计
                   [
                       'type' => 'LEFT',
                       'table' => "(SELECT qr_id, COUNT(*) as count, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE YEARWEEK(date_format(addtime,'%Y-%m-%d')) = YEARWEEK(now())
                                  GROUP BY qr_id) week",
                       'on' => 'q.id = week.qr_id'
                   ],
                   // 本周成功订单
                   [
                       'type' => 'LEFT', 
                       'table' => "(SELECT qr_id, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE YEARWEEK(date_format(addtime,'%Y-%m-%d')) = YEARWEEK(now()) AND status = 1
                                  GROUP BY qr_id) week_succ",
                       'on' => 'q.id = week_succ.qr_id'
                   ],
                   // 本月订单统计
                   [
                       'type' => 'LEFT',
                       'table' => "(SELECT qr_id, COUNT(*) as count, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE DATE_FORMAT(addtime, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
                                  GROUP BY qr_id) month",
                       'on' => 'q.id = month.qr_id'
                   ],
                   // 本月成功订单
                   [
                       'type' => 'LEFT',
                       'table' => "(SELECT qr_id, SUM(money) as money 
                                  FROM pre_order 
                                  WHERE DATE_FORMAT(addtime, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') AND status = 1
                                  GROUP BY qr_id) month_succ",
                       'on' => 'q.id = month_succ.qr_id'
                   ]
               ]
           ]
       );
       
       if(empty($result['count'])) {
           exitjson(0, 'ok', ['count' => 0, 'data' => []]);
       }
       
       $data = [];
       foreach($result['result'] as $row) {
           $data[] = [
               'id' => $row['id'],
               'today_count' => intval($row['today_count']),
               'today_money' => number_format($row['today_money'], 2),
               'today_succmoney' => number_format($row['today_succmoney'], 2),
               'week_count' => intval($row['week_count']),
               'week_money' => number_format($row['week_money'], 2),
               'week_succmoney' => number_format($row['week_succmoney'], 2),
               'month_count' => intval($row['month_count']),
               'month_money' => number_format($row['month_money'], 2),
               'month_succmoney' => number_format($row['month_succmoney'], 2)
           ];
       }
       
       exitjson(0, 'ok', [
           'count' => count($data),
           'data' => $data
       ]);
    }
    public function qrliststatus(){
        if($this->islogin_user !== 1) exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $qr_row = $this->db->qrlistrow($id,'id','`id`,`status`,`pid`');
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在');
        }
        $params = [
            'status'=>$qr_row['status'] == 1 ? 0 : 1
        ];
        if($this->db->update('qrlist',$params,['id'=>$id])):
            createLog('修改二维码状态', http_ip(),$this->userrow['user']);
            exitjson(200,'修改二维码状态成功');
        endif;
        exitjson(-1,'修改失败！');
    }
}
?>